#include "../include/clientEntrance.h"

bool is_valid_button(int value){
    return (value >= REGISTER && value <= LOGOUT);
}

Button get_button_input(){
    int input_num;
    char ch;
    while(1){
        printf("请输入要进行的操作\n"
                "注册 1 \n"
                "登录 2 \n"
                "注销 3 \n");

        if(scanf("%d", &input_num) == 1){
            if(scanf("%c", &ch) == 1 && ch != '\n') {
                while(getchar() != '\n');
                printf("非法输入！请重新输入！\n");
            }
            else if(is_valid_button(input_num)){
                break;
            }
            else{
                printf("非法输入！请重新输入！\n");
            }
        }
        else{
            while(getchar() != '\n');
            printf("非法输入！请重新输入！\n");
        }
    }
    return (Button)input_num;
}
int client_entrance(int sockfd){
    Button button = get_button_input();
    send(sockfd, &button, sizeof(Button), 0);
    /* printf("button = %d\n",button); */
    switch(button){
    case REGISTER:
        client_register(sockfd);
        break;
    case LOGIN:
        client_login(sockfd);
        break;
    case LOGOUT:
        client_logout(sockfd);
        break;
    }

    return 0;
}

int send_train(int sockfd, char *buf){
    logTrain_t train;
    bzero(train.data, sizeof(train.data));
    train.length = strlen(buf) + 1;
    strcpy(train.data, buf);
    send(sockfd, &train.length, sizeof(train.length) ,0);
    send(sockfd, train.data, train.length, 0); 
    return 0;
}
int recv_train(int sockfd, char *buf){
    logTrain_t train;
    bzero(train.data, sizeof(train.data));
    recv(sockfd, &train.length, sizeof(train.length) , MSG_WAITALL);
    recv(sockfd, train.data, train.length, MSG_WAITALL);
    strcpy(buf, train.data);
    return 0;
}
int client_register(int sockfd){
    char username[1024] = {0};
    char password[1024] = {0};
    char salt[1024] = {0};
    char response[1024] = {0};

    printf("请输入用户名：");
    fgets(username, sizeof(username), stdin);
    // todo用户名含空格错误处理                                                        
    username[strcspn(username, "\n")] = '\0';
    /* printf("username = %s\n", username); */

    // 发送用户名火车
    send_train(sockfd, username);

    // 接收盐值
    recv_train(sockfd, salt);
    /* printf("salt = %s\n", salt); */

    // 清空输入缓冲区的剩余字符
    /* int c; */
    /* while((c = getchar()) != '\n' && c != EOF); */

    printf("请输入密码：");
    fgets(password, sizeof(password), stdin);
    password[strcspn(password, "\n")] = '\0';
    /* printf("password = %s\n", password); */


    // 进行加密
    char *encryptedPWD = crypt(password, salt);
    ERROR_CHECK(encryptedPWD, NULL, "crypt");
    printf("encryptedPWD = %s\n", encryptedPWD);

    // 将密文密码发给服务端
    send_train(sockfd, encryptedPWD);

    // 接收服务端返回的结果
    recv_train(sockfd, response);
    printf("response = %s,请退出进行登录\n", response);
    

    return 0;
}

int client_login(int sockfd){
    printf("开始登录\n");
    char username[1024] = {0};
    char password[1024] = {0};
    char salt[1024] = {0};
    int isExist = 0;
    int isSuccess = 0;
    while(!isSuccess){
        printf("请输入用户名：");
        fgets(username, sizeof(username), stdin);
        // todo用户名含空格错误处理                                                        
        username[strcspn(username, "\n")] = '\0';
        /* printf("username = %s\n", username); */

        // 清空输入缓冲区的剩余字符
        /* int c; */
        /* while((c = getchar()) != '\n' && c != EOF); */

        // 发送用户名火车
        send_train(sockfd, username);

        // 接收服务端响应
        recv(sockfd, &isExist, sizeof(isExist), 0);

        if(isExist == 0){
            printf("用户不存在！\n");
            continue;
        }
        else{
            // 接收盐值
            recv_train(sockfd, salt);
            /* printf("salt = %s\n", salt); */
            while(1){
                printf("请输入密码：");
                fgets(password, sizeof(password), stdin);
                password[strcspn(password, "\n")] = '\0';
                /* printf("password = %s\n", password); */

                // 进行加密
                char *encryptedPWD = crypt(password, salt);
                ERROR_CHECK(encryptedPWD, NULL, "crypt");
                /* printf("encryptedPWD = %s\n", encryptedPWD); */

                // 将密文密码发给服务端
                send_train(sockfd, encryptedPWD);

                // 接收服务端响应
                recv(sockfd, &isSuccess, sizeof(isSuccess), 0);

                if(isSuccess == 0){
                    printf("密码错误！\n");
                    continue;
                }
                else{
                    printf("登陆成功！\n");
                    printf("=================================\n");
                    printf("可以执行以下命令:\n");
                    printf("ls:    列出当前目录下的文件\n");
                    printf("cd:    切换当前工作目录\n");
                    printf("pwd:   打印当前工作目录\n");
                    printf("remove:删除指定空目录\n");
                    printf("mkdir: 创建目录\n");
                    printf("gets:  下载文件\n");
                    printf("puts:  上传文件\n");
                    printf("=================================\n");
                    break;
                }
            }
        }
    }
    return 0;
}

int client_logout(int sockfd){
    char username[1024] = {0};
    char password[1024] = {0};
    char salt[1024] = {0};
    char response[1024] = {0};
    int isExist = 0;

    printf("请输入用户名：");
    fgets(username, sizeof(username), stdin);
    // todo用户名含空格错误处理                                                        
    username[strcspn(username, "\n")] = '\0';
    printf("username = %s\n", username);

    // 清空输入缓冲区的剩余字符
    /* int c; */
    /* while((c = getchar()) != '\n' && c != EOF); */

    printf("请输入密码：");
    fgets(password, sizeof(password), stdin);
    password[strcspn(password, "\n")] = '\0';
    printf("password = %s\n", password);

    // 发送用户名火车
    send_train(sockfd, username);
    // 接收服务端响应
    recv(sockfd, &isExist, sizeof(isExist), 0);
    if(isExist == 0){
        printf("注销失败！\n");
    }
    else{
        // 接收盐值
        recv_train(sockfd, salt);
        printf("salt = %s\n", salt);

        // 进行加密
        char *encryptedPWD = crypt(password, salt);
        ERROR_CHECK(encryptedPWD, NULL, "crypt");
        printf("encryptedPWD = %s\n", encryptedPWD);

        // 将密文密码发给服务端
        send_train(sockfd, encryptedPWD);

        // 接收服务端返回的结果
        recv_train(sockfd, response);
        printf("response = %s\n", response);
    }
    return 0;
}

/* int main(void){ */
/*     int sockfd = 1; */
/*     client_entrance(sockfd); */
/*     return 0; */
/* } */

